﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

using YEngineLib;
using YEngineLib.Graphics;
using YEngineLib.Input;
using YEngineLib.State;
using YEngineLib.Util;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using System.Threading;
using Spyfall.Objects;

namespace Spyfall.States
{
    class OptionState : YBaseGameState
    {
        GamePadState currentGamePadState;
        GamePadState previousGameState;
        public PlayerIndex daPlaya;
        private Managers.ObjectManager objectManager;
        private Managers.MessageManager msgManager;
        Vector2 screenPosition;
        SpriteFont spriteFont;
        private Viewport viewPort;
        List<String> menuOptions = new List<String>();
        int selectedMenuOption = 1;

        public OptionState()
        {
            screenPosition = new Vector2(10.0f, 30.0f);
            objectManager = new Managers.ObjectManager();
            msgManager = new Managers.MessageManager();
        }

        public override void LoadContent()
        {
            LoadPercentage = 100.0f;
            spriteFont = YEngine.Game.Content.Load<SpriteFont>("Fonts/SpriteFont1");
        }

        public override void OnEnter(string previousState)
        {
            currentGamePadState = GamePad.GetState(daPlaya);
            viewPort = YEngine.Graphics.ScreenViewport;
            menuOptions.Add("TEST");
        }

        public override void Input()
        {
            previousGameState = currentGamePadState;
            currentGamePadState = GamePad.GetState(daPlaya);
            //if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGameState.Buttons.A != ButtonState.Pressed)
            //{
            //    YEngine.State.ChangeState("MenuState");
            //    return;
            //}

            //if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
            //{
            //    YEngine.State.ChangeState("MenuState");
            //    return;
            //}


            if (YEngine.Input.Keyboard.KeyPressed(Keys.Down))
            {
                if (selectedMenuOption == 5)    //theres a Wrap function.. check that out later.
                {
                    selectedMenuOption = 0;
                }
                selectedMenuOption += 1;
            }
            if (YEngine.Input.Keyboard.KeyPressed(Keys.Up))
            {
                if (selectedMenuOption == 1)
                {
                    selectedMenuOption = 6;
                }
                selectedMenuOption -= 1;
            }

            if ((previousGameState.IsButtonUp(Buttons.LeftThumbstickDown) && currentGamePadState.IsButtonDown(Buttons.LeftThumbstickDown)))
            {
                if (selectedMenuOption == 5)
                {
                    selectedMenuOption = 0;
                }
                selectedMenuOption += 1;
            }

            if ((previousGameState.IsButtonUp(Buttons.LeftThumbstickUp) && currentGamePadState.IsButtonDown(Buttons.LeftThumbstickUp)))
            {
                if (selectedMenuOption == 1)
                {
                    selectedMenuOption = 6;
                }
                selectedMenuOption -= 1;
            }
//-----------------------------------------------------------------------



            if (selectedMenuOption == 1)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Home))
                {
                    YEngine.State.ChangeState("MenuState");
                    return;
                }
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    YEngine.State.ChangeState("CreditsState");
                    return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGameState.Buttons.A != ButtonState.Pressed)
                {
                    YEngine.State.ChangeState("CreditsState");
                    return;
                }
            }
            if (selectedMenuOption == 2)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    YEngine.State.ChangeState("HighScoreState");
                    return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGameState.Buttons.A != ButtonState.Pressed)
                {
                    YEngine.State.ChangeState("HighScoreState");
                    return;
                }
            }
            if (selectedMenuOption == 3)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    YEngine.State.ChangeState("FileManagerState");
                    return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGameState.Buttons.A != ButtonState.Pressed)
                {
                    YEngine.State.ChangeState("FileManagerState");
                    return;
                }
            }
            if (selectedMenuOption == 4)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    YEngine.State.ChangeState("HowToPlayState");
                    return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGameState.Buttons.A != ButtonState.Pressed)
                {
                    YEngine.State.ChangeState("HowToPlayState");
                    return;
                }
            }
            if (selectedMenuOption == 5)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    YEngine.State.ChangeState("ShopState");
                    return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGameState.Buttons.A != ButtonState.Pressed)
                {
                    YEngine.State.ChangeState("ShopState");
                    return;
                }
            }



        }

        public override void Update()
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                YEngine.Game.Exit();
            objectManager.UpdateObjects((float)YEngine.Time.GameTime.ElapsedGameTime.TotalSeconds);
        }

        public override void Draw()
        {
            YEngine.Graphics.SpriteBatch.Begin();
            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Options Prototype - Home key brings back to Main Menu", new Vector2(10.0f, 600.0f), Color.White);

            if (selectedMenuOption == 1)
            {
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Credits Prototype", new Vector2(10.0f, 30.0f), Color.Black);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Highscore Prototype", new Vector2(10.0f, 50.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Save / Load Prototype", new Vector2(10.0f, 70.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "How To Play Prototype", new Vector2(10.0f, 90.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Shop Prototype", new Vector2(10.0f, 110.0f), Color.White);
            }
            if (selectedMenuOption == 2)
            {
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Credits Prototype", new Vector2(10.0f, 30.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Highscore Prototype", new Vector2(10.0f, 50.0f), Color.Black);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Save / Load Prototype", new Vector2(10.0f, 70.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "How To Play Prototype", new Vector2(10.0f, 90.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Shop Prototype", new Vector2(10.0f, 110.0f), Color.White);
            }
            if (selectedMenuOption == 3)
            {
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Credits Prototype", new Vector2(10.0f, 30.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Highscore Prototype", new Vector2(10.0f, 50.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Save / Load Prototype", new Vector2(10.0f, 70.0f), Color.Black);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "How To Play Prototype", new Vector2(10.0f, 90.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Shop Prototype", new Vector2(10.0f, 110.0f), Color.White);
            }
            if (selectedMenuOption == 4)
            {
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Credits Prototype", new Vector2(10.0f, 30.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Highscore Prototype", new Vector2(10.0f, 50.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Save / Load Prototype", new Vector2(10.0f, 70.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "How To Play Prototype", new Vector2(10.0f, 90.0f), Color.Black);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Shop Prototype", new Vector2(10.0f, 110.0f), Color.White);
            }
            if (selectedMenuOption == 5)
            {
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Credits Prototype", new Vector2(10.0f, 30.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Highscore Prototype", new Vector2(10.0f, 50.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Save / Load Prototype", new Vector2(10.0f, 70.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "How To Play Prototype", new Vector2(10.0f, 90.0f), Color.White);
                YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Shop Prototype", new Vector2(10.0f, 110.0f), Color.Black);
            }

            YEngine.Graphics.SpriteBatch.End();
        }
    }
}
